package cn.cqie.attendance.common.config;

import cn.cqie.attendance.common.filter.JwtFilter;
import cn.cqie.attendance.common.realm.UserRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SubjectFactory;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public SubjectFactory subjectFactory() {
        return new JwtDefaultSubjectFactory();
    }

    /**
     * 配置shiro加密器, 加密方式为md5
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        matcher.setHashIterations(2);
        return matcher;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(
            UserRealm userRealm,
            HashedCredentialsMatcher hashedCredentialsMatcher) {

        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();

        DefaultWebSecurityManager defaultWebSecurityManager =
                new DefaultWebSecurityManager();

        userRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        defaultWebSecurityManager.setRealm(userRealm);

        // 关闭shiro自带的session
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator =
                new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        defaultWebSecurityManager.setSubjectDAO(subjectDAO);

        // 禁止Subject的getSession方法
        defaultWebSecurityManager.setSubjectFactory(subjectFactory());

        shiroFilter.setSecurityManager(defaultWebSecurityManager);

        // 过滤链
        Map<String, Filter> customFiltersMap = new HashMap<>();
        customFiltersMap.put("jwt", new JwtFilter());
        shiroFilter.setFilters(customFiltersMap);

        // 拦截链
        Map<String, String> filterMap = new LinkedHashMap<>();
        // 开放knife4j
        filterMap.put("/doc.html", "anon");
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/v2/**", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        // 开放验证码
        filterMap.put("/kaptcha", "anon");
        // 开放登陆判断接口
        filterMap.put("/loginSub", "anon");
        filterMap.put("/**", "jwt");
        // 设置拦截规则
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

}
